﻿using System;
using System.Web.UI.WebControls;
using BoldBrick.OneIntranet.HR.DataObjects.Lists;
using BoldBrick.OneIntranet.HR.Resources;
using BoldBrick.OneIntranet.HR.WebParts.HRWorkflowStatisticsWebPart;
using Microsoft.SharePoint;
using Mossquito.Moss.SharePoint.CommonControls.Utils;
using BoldBrick.SharePoint.CommonControls.Extensions;
using System.Linq;

namespace BoldBrick.OneIntranet.HR.WebParts
{
	public class HRWorkflowStatisticsEditorPart : BaseEditorPart
	{
		#region ------ Controls -------------------------------------------------------------------

		private DropDownList dropDownListWorkflows;
		DropDownList dropDownListWorkflowStatus;

		protected override void CreateChildControls()
		{
			base.CreateChildControls();

			uint language = SPContext.Current.Web.Language;

			// dropDownListWorkflows
			string headerText = ResourceBuilder.GetResource("HRWorkflowStatisticsEditorPart_dropDownListWorkflows_Title", language);
			dropDownListWorkflows = EditorPartHelper.AddPropertyControl<DropDownList>(this, "dropDownListWorkflows", headerText);
			LoadDropDownListWorkflows();

			// dropDownListWorkflowStatus
			headerText = ResourceBuilder.GetResource("HRWorkflowStatisticsEditorPart_dropDownListWorkflowStatus_Title", language);
			dropDownListWorkflowStatus = EditorPartHelper.AddPropertyControl<DropDownList>(this, "dropDownListWorkflowStatus", headerText);
			LoadDropDownListWorkflowStatus();
		}

		#endregion

		#region ------ Internals: Data loading ----------------------------------------------------

		/// <summary>
		/// Loads items into dropDownListWorkflows
		/// </summary>
		private void LoadDropDownListWorkflows()
		{
			if (dropDownListWorkflows.Items.Count > 0)
				return;

			uint language = SPContext.Current.Web.Language;

			SPList listWorkflows = SPContext.Current.Web.GetList(Lists.WorkflowsList);
			if (listWorkflows == null)
			{
				string errorMessage = ResourceBuilder.GetResource("HRWorkflowStatisticsEditorPart_Error_WorkflowsListNotFound", language);
				ShowError(errorMessage);
				return;
			}

			ListItem[] listItems = listWorkflows.RootFolder.SubFolders.OfType<SPFolder>()
				.Select(folder => folder.Item)
				.Where(item => item != null)
				.Select(item => new ListItem(item.Title, item.ID.ToString()))
				.OrderBy(listItem => listItem.Text)
				.ToArray();

			this.dropDownListWorkflows.Items.AddRange(listItems);
		}

		/// <summary>
		/// Loads items into dropDownListWorkflowStatus
		/// </summary>
		private void LoadDropDownListWorkflowStatus()
		{
			if (dropDownListWorkflowStatus.Items.Count > 0)
				return;

			uint language = SPContext.Current.Web.Language;

			Array values = Enum.GetValues(typeof(WorkflowStatus));
			ListItem[] listItems = values.OfType<WorkflowStatus>()
				.Select(value => new ListItem(ResourceBuilder.GetResource("HRWorkflowStatisticsEditorPart_WorkflowStatus_" + value, language), value.ToString()))
				.ToArray();
			
			this.dropDownListWorkflowStatus.Items.AddRange(listItems);
		}

		#endregion

	    #region ------ Internals: EditorPart overrides --------------------------------------------

	    public override bool ApplyChanges()
	    {
	        EnsureChildControls();

	        HRWorkflowStatisticsWebPart.HRWorkflowStatisticsWebPart webPart = this.WebPartToEdit as HRWorkflowStatisticsWebPart.HRWorkflowStatisticsWebPart;
	        if (webPart == null)
	            return true;

	        if (this.dropDownListWorkflows.SelectedItem != null)
	        {
	            webPart.WorkflowID = Convert.ToInt32(this.dropDownListWorkflows.SelectedValue);
	        }

	        if (this.dropDownListWorkflowStatus.SelectedItem != null)
	        {
	            webPart.WorkflowStatus = (WorkflowStatus) Enum.Parse(typeof (WorkflowStatus), this.dropDownListWorkflowStatus.SelectedValue);
	        }

	        return true;
	    }

	    public override void SyncChanges()
	    {
	        EnsureChildControls();

	        HRWorkflowStatisticsWebPart.HRWorkflowStatisticsWebPart webPart = this.WebPartToEdit as HRWorkflowStatisticsWebPart.HRWorkflowStatisticsWebPart;
	        if (webPart == null)
	            return;

	        this.dropDownListWorkflows.SelectedValue = webPart.WorkflowID.ToString();
	        this.dropDownListWorkflowStatus.SelectedValue = webPart.WorkflowStatus.ToString();
	    }

	    #endregion
	}
}
